package org.qiuer.func.function.array;

import org.qiuer.ast.expression.function.ArrowFunctionExpression;
import org.qiuer.func.AMemberFunction;
import org.qiuer.run.Context;
import org.qiuer.exception.EValidate;
import org.qiuer.exception.IException;

import java.util.ArrayList;
import java.util.List;

public class ArrayMapFunction extends AMemberFunction<List> {

  @Override
  protected Object run(List member, Context context, Object... args) throws IException {
    ArrowFunctionExpression f = EValidate.cast(args[0], ArrowFunctionExpression.class);

    List<Object> ret = new ArrayList<>();
    for (int i = 0; i < member.size(); i++) {
      Object[] tmp = new Object[3];
      tmp[0] = member.get(i);
      tmp[1] = (long) i;
      tmp[2] = member;
      Object a = f.run(context, tmp);
      ret.add(a);
    }
    return ret;
  }

  @Override
  public String getName() {
    return "map";
  }
}